import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:mahjong/card_entity.dart';
import 'package:mahjong/const/MahjongConst.dart';
import 'package:mahjong/const/MahjongSystemConst.dart';
import 'package:mahjong/entity/UserTouchBar.dart';
import 'package:mahjong/entity/card/MahjongCardEntity.dart';
import 'package:mahjong/notify/LeftUserInfo.dart';
import 'package:mahjong/widget/LeftGameCommons.dart';
import 'package:mahjong/widget/MahjongCommon.dart';
import 'package:provider/provider.dart';
import 'SwapMahjongWidget.dart';

class LeftGameWidget extends StatelessWidget {
  const LeftGameWidget({
    Key key,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Consumer(builder:
        (BuildContext buildContext, LeftUserInfo provider, Widget child) {
      if (provider.userId == null) {
        return Container();
      } else {
        return Stack(
          children: [
            // 头像的位置
            Positioned(
                top: ScreenUtil().screenHeight / 3,
                left: MahjongSystemConst.edgeMargin,
                child: SafeArea(
                  key: userLocation[provider.userId].headKey,
                  child: MahjongCommon.getUserHeader(
                    provider,
                  ),
                )),
            initReading(provider.prepare),

            provider.listening
                ? Positioned(
                    top: ScreenUtil().screenHeight / 3 -
                        MahjongSystemConst.actionTipsWidth,
                    left: MahjongSystemConst.edgeMargin,
                    child: SafeArea(
                        child: Image.asset(
                      "images/mahjong/action/CARD_CALL_TIPS.png",
                      height: MahjongSystemConst.actionTipsWidth,
                      width: MahjongSystemConst.actionTipsWidth,
                      fit: BoxFit.cover,
                    )))
                : Container(),
            // 出牌的位置
            Positioned(
                top: MahjongSystemConst.lrbtMagin,
                right: MahjongSystemConst.lrlrMagin,
                child: RotationTransition(
                    turns: new AlwaysStoppedAnimation(90 / 360),
                    child:
                        MahjongCommon.getUserExportCard(provider, resv: true))),
            // 手牌位置
            // 手上的牌的位置
            // 手牌位置
            Positioned(
                top: 30.h,
                right: MahjongSystemConst.lrUserCardMargin,
                child: Container(
                    alignment: Alignment.topRight,
                    width: MahjongSystemConst.lrUserCardWidth *
                        MahjongSystemConst.cardWHScale *
                        2,
                    height: MahjongSystemConst.lrCardWidth *
                        MahjongSystemConst.cardLrMultiple,
                    child: Stack(
                      children: buildUserCard(provider),
                    ))),
            SwapMahjongWidget(
              swap: provider.swap,
              position: UserPosition.LEFT,
            ),
            Positioned(
              right: MahjongSystemConst.lrlrMagin +
                  MahjongSystemConst.exportAreaSize -
                  MahjongSystemConst.actionTipsWidth,
              top: ScreenUtil().screenHeight / 2 -
                  MahjongSystemConst.actionTipsWidth,
              child: AnimatedOpacity(
                opacity:
                    (provider.userDoAction != null && provider.showDoAction)
                        ? 1
                        : 0,
                duration: Duration(seconds: 1),
                child: provider.showDoAction
                    ? Image.asset(
                        "images/mahjong/action_tips/" +
                            provider.userDoAction +
                            ".png",
                        height: MahjongSystemConst.actionTipsWidth,
                        width: MahjongSystemConst.actionTipsWidth,
                        fit: BoxFit.cover,
                      )
                    : SizedBox(),
              ),
            ),
          ],
        );
      }
    });
  }

  List<Widget> buildUserCard(LeftUserInfo provider) {
    List<UserTouchBar> data = provider.userTouchBar;
    var number = provider.cardCount;
    List<Widget> result = [];
    var exporTag = (number % 3 == 2);
    double margin = 0;
    var top = MahjongSystemConst.lrUserCardWidth;
    if (data != null && data.length > 0) {
      for (int i = 0; i < data.length; i++) {
        if (i > 0) {
          margin = margin + top * 1.5;
        }
        int maxNumber = 3;
        for (int j = 0; j < maxNumber; j++) {
          int cardId = data[i].cards[j].cardId;
          double cardLeft = 0;
          if (data[i].cards.length == 4 &&
              j != 1 &&
              data[i].cardEnum == CardEnum.ANGANG) {
            cardId = null;
          } else if (data[i].cards.length == 4 &&
              j == 1 &&
              data[i].cardEnum == CardEnum.ANGANG) {
            result.add(Positioned(
                top: margin + top * j,
                right: 0,
                child: RotationTransition(
                    turns: new AlwaysStoppedAnimation(270 / 360),
                    child: Container(
                        width: MahjongSystemConst.lrUserCardWidth *
                            MahjongSystemConst.cardWHScale,
                        child: LeftGameCommons.buildExportCard(
                            MahjongCardEntity(cardId: null),
                            MahjongSystemConst.lrUserCardWidth)))));
            cardLeft = MahjongSystemConst.lrUserCardWidth *
                MahjongSystemConst.cardWHScale *
                MahjongSystemConst.cardOtherScale;
          }

          result.add(Positioned(
              top: margin + top * j,
              right: cardLeft,
              child: RotationTransition(
                  turns: cardId == null
                      ? new AlwaysStoppedAnimation(270 / 360)
                      : new AlwaysStoppedAnimation(90 / 360),
                  child: Container(
                      width: MahjongSystemConst.lrUserCardWidth *
                          MahjongSystemConst.cardWHScale,
                      child: LeftGameCommons.buildExportCard(
                          MahjongCardEntity(cardId: cardId),
                          MahjongSystemConst.lrUserCardWidth)))));
          if (data[i].cards.length == 4 &&
              j == 1 &&
              data[i].cardEnum != CardEnum.ANGANG) {
            result.add(Positioned(
                top: margin + top * j,
                right: MahjongSystemConst.lrUserCardWidth *
                    MahjongSystemConst.cardWHScale *
                    MahjongSystemConst.cardOtherScale,
                child: RotationTransition(
                    turns: AlwaysStoppedAnimation(90 / 360),
                    child: Container(
                        width: MahjongSystemConst.lrUserCardWidth *
                            MahjongSystemConst.cardWHScale,
                        child: LeftGameCommons.buildExportCard(
                            MahjongCardEntity(cardId: cardId),
                            MahjongSystemConst.lrUserCardWidth)))));
          }
          if (j == maxNumber - 1) {
            margin = margin + top * j;
          }
        }
      }
      margin += 2 * top;
    }
    top = MahjongSystemConst.lrCardWidth;
    for (int i = 0; i < number; i++) {
      if (exporTag && i == number - 1) {
        margin += top * 2;
      } else if (i != 0) {
        margin += top;
      }
      if (provider.win) {
        result.add(Positioned(
          top: margin,
          right: 0,
          child: Container(
            width: MahjongSystemConst.lrUserCardWidth *
                MahjongSystemConst.cardWHScale,
            child: Image.asset(
              "images/mahjong/user/left/bg_win.png",
              fit: BoxFit.contain,
            ),
          ),
        ));
      } else {
        result.add(Positioned(
          top: margin,
          right: 0,
          child: Container(
            width: top,
            child: Image.asset(
              "images/mahjong/user/left/usercard.png",
              fit: BoxFit.contain,
            ),
          ),
        ));
      }
    }
    if (provider.win) {
      result.add((Positioned(
          top: margin + top * 1.5,
          right: 0,
          child: RotationTransition(
              turns: new AlwaysStoppedAnimation(90 / 360),
              child: Container(
                width: top * MahjongSystemConst.cardWHScale,
                child: LeftGameCommons.buildExportCard(
                    MahjongCardEntity(cardId: provider.huCard), top),
              )))));
    }

    return result;
  }

  initReading(bool prepare) {
    return prepare
        ? Positioned(
            top: ScreenUtil().screenHeight / 3,
            left: MahjongSystemConst.edgeMargin +
                MahjongSystemConst.userHeaderImageWidth,
            child: SafeArea(
              child: Container(
                height: MahjongSystemConst.userHeaderImageWidth *
                    MahjongSystemConst.cardMinWHScale,
                width: MahjongSystemConst.userHeaderImageWidth *
                    MahjongSystemConst.cardMinWHScale,
                child: Image.asset(
                  "images/mahjong/commons/reading.png",
                  fit: BoxFit.contain,
                ),
              ),
            ))
        : SizedBox();
  }
}
